from django.contrib import admin
from django.utils.html import format_html
from .models import RentInfo, Tenant
from django.contrib import messages


# Register your models here.

class InfoAdmin(admin.ModelAdmin):
    # change_list_template = 'change_list.html'  # 重写模版

    list_display = ('region', 'location', 'brand', 'is_sharing', 'deposit', 'has_elevator', 'sharing_num', 'kitchen',
                    'bathroom', 'rent', 'area', 'allow_pet', 'water_fee', 'electric_fee', 'surroundings',
                    'recommend_index', 'comment', 'update_at', 'edit_button', 'delete_button')
    list_display_links = ('edit_button',)
    list_filter = ('region', 'location', 'brand',)
    list_per_page = 10  # 每页显示10条记录
    save_on_top = True  # 保存和取消按钮是否在顶部 True为在顶部，False为在底部
    ordering = ('-update_at',)
    actions_on_top = True
    actions_on_bottom = True
    actions = ["copy", "board", "search_ip"]

    def search_ip(self, request, queryset):
        pass

    search_ip.short_description = "ip"
    search_ip.type = 'primary'
    # board.style = 'color:blue;'
    search_ip.icon = 'el-icon-search'
    # search_ip.confirm = '确定要进入看板吗？'
    search_ip.action_type = 1
    search_ip.action_url = '../info/get_ip/'  # 查询ip

    # 添加看板按钮
    def board(self, request, queryset):
        pass

    board.short_description = "看板"
    board.type = 'primary'
    # board.style = 'color:blue;'
    board.icon = 'el-icon-view'
    board.confirm = '确定要进入看板吗？'
    board.action_type = 1
    board.action_url = '../info/index/'  # 看板路由地址

    # 编辑/查看单行数据
    def edit_button(self, obj):
        return format_html(
            f'<a  class= "el-button el-button--primary el-button--small el-icon-edit" '
            f'onclick="href=\'/admin/info/rentinfo/{obj.id}/change/\'">编辑</a>')

    edit_button.short_description = '编辑'

    # 删除单行数据
    def delete_button(self, obj):
        return format_html(
            '<a class = "el-button stop-submit el-button--danger el-button--small el-icon-delete" '
            'onclick="window.location.href=\'/admin/info/rentinfo/{}/delete/\'">删除</a>',
            obj.id)

    delete_button.short_description = '删除'
    # 自动转义HTML标签，使链接生效
    edit_button.allow_tags = True
    delete_button.allow_tags = True

    # 复制数据
    def copy(self, request, queryset):
        # 复制一条数据
        id = request.POST.get('_selected_action')
        data = RentInfo.objects.get(id=id)
        RentInfo.objects.create(
            region=data.region,
            location=data.location,
            brand=data.brand,
            is_sharing=data.is_sharing,
            deposit=data.deposit,
            has_elevator=data.has_elevator,
            sharing_num=data.sharing_num,
            kitchen=data.kitchen,
            bathroom=data.bathroom,
            rent=data.rent,
            area=data.area,
            allow_pet=data.allow_pet,
            water_fee=data.water_fee,
            electric_fee=data.electric_fee,
            surroundings=data.surroundings,
            recommend_index=data.recommend_index,
            comment=data.comment,
            update_at=data.update_at
        ).save()
        messages.success(request, '复制成功！')

    copy.short_description = "复制"
    copy.type = 'primary'
    copy.icon = 'el-icon-copy-document'
    copy.confirm = '确定复制选中项吗？'


class TenantAdmin(admin.ModelAdmin):
    list_display = ('name', 'phone', 'id_card', 'gender', 'age', 'education', 'marriage', 'hometown',
                    'current_address', 'work_address', 'company', 'update_at', 'edit_button', 'delete_button')
    list_filter = ('name', 'phone', 'id_card',)
    list_display_links = ('edit_button',)
    list_per_page = 10
    ordering = ('-update_at',)
    save_on_top = True

    def edit_button(self, obj):
        return format_html(
            f'<a  class= "el-button el-button--primary el-button--small el-icon-edit" '
            f'onclick="href=\'/admin/info/tenant/{obj.id}/change/\'">编辑</a>')

    edit_button.allow_tags = True
    edit_button.short_description = '编辑'

    def delete_button(self, obj):
        return format_html(
            f'<a  class = "el-button stop-submit el-button--danger el-button--small el-icon-delete" '
            f'onclick="href=\'/admin/info/tenant/{obj.id}/delete/\'">删除</a>')

    delete_button.allow_tags = True
    delete_button.short_description = '删除'


admin.site.register(RentInfo, InfoAdmin)
admin.site.register(Tenant, TenantAdmin)

admin.site.site_title = '租房信息共享平台'
admin.site.site_header = '租房信息共享平台'
admin.site.index_title = '租房信息共享'
